package com.example.dp.jiemian.bt2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dp.jiemian.R;

import java.text.SimpleDateFormat;
import java.util.List;

public class bt2 extends AppCompatActivity {

    private TextView tv1,tv2;
    private EditText et1,et2;
    private Button bt1;
    private String ip;
    private String string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bt2);
        tv1 = findViewById(R.id.bt2_tv1);
        tv2 = findViewById(R.id.bt2_tv2);
        et1 = findViewById(R.id.bt2_et1);
        et2 = findViewById(R.id.bt2_et2);
        initWifiIp();
        tv1.setText(ip);
        initGPS();
        tv2.setText(string);
        findViewById(R.id.bt2_bt1).setOnClickListener(v -> {
            initWifiIp();
            initGPSPK();
            tv1.setText(ip);
            initGPS();
            tv2.setText(string);
        });
    }


    private LocationManager locationManager;
    private String provider;

    //获取当前GPS定位
    private void initGPS() {
        //获取定位服务
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //获取当前可用的位置控制器
        List<String> list = locationManager.getProviders(true);
        if (list.contains(LocationManager.GPS_PROVIDER)) {
            //是否为GPS位置控制器
            provider = LocationManager.GPS_PROVIDER;
        }
        else if (list.contains(LocationManager.NETWORK_PROVIDER)) {
            //是否为网络位置控制器
            provider = LocationManager.NETWORK_PROVIDER;

        } else {
            Toast.makeText(this, "请检查网络或GPS是否打开",
                    Toast.LENGTH_LONG).show();
            return;
        }
        @SuppressLint("MissingPermission")
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            //获取当前位置，这里只用到了经纬度
            string = "纬度为：" + location.getLatitude() + ",经度为："
                    + location.getLongitude();
        }
    }


    //获取当前IP地址
    private void initWifiIp() {
        //获取wifi服务
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        ip = intToIp(ipAddress);
    }
    private String intToIp(int i) {
        //获取当前IP地址
        return (i & 0xFF ) + "." +
                ((i >> 8 ) & 0xFF) + "." +
                ((i >> 16 ) & 0xFF) + "." +
                ( i >> 24 & 0xFF) ;
    }

    //获取当前wifi名字
    private String initWifi(){
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        Log.d("wifiInfo", wifiInfo.toString());
        Log.d("SSID",wifiInfo.getSSID());
        return wifiInfo.getSSID();
    }

    //获取当前wifi mac地址
    private String getLocalMacAddress() {
        WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    private String mMockProviderName = LocationManager.GPS_PROVIDER;
    SimpleDateFormat formatter;
    long time;
    String formatTime;
    
    private void initGPSPK() {

    }


}
